//
//  HJSellOderViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSellOderViewController.h"
#import "HJSellOderHeader.h"
#import "HJSellOderBottom.h"
#import "HJHomeModel.h"
#import "HJCodeView.h"
#import "HJPayViewController.h"

@interface HJSellOderViewController ()
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)HJOderModel *oderModel;
@property(nonatomic,strong)HJSellOderHeader *header;
@property(nonatomic,strong)HJSellOderBottom *bottom;
@property(nonatomic,assign)NSInteger currentTag;
@property(nonatomic,strong)NSString * days;
@property(nonatomic,strong)NSString * shoes_cost;
@property(nonatomic,assign)BOOL select_Btn;
@end

@implementation HJSellOderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [mNotificationCenter addObserver:self selector:@selector(codeBtnNotification:) name:@"codeBtnNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(loadOrderAddNotification:) name:@"loadOrderAddNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(loadOrderNot:) name:@"loadOrderNot" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(textFieldNotification:) name:@"textFieldNotification" object:nil];
    [self configScroll];
    [self loadDataWithWork];
    self.days = @"3";
    self.select_Btn = NO;
}

-(void)loadDataWithWork {
    NSMutableDictionary * params =[NSMutableDictionary dictionary];
    params[@"shoes_id"] = self.shoes_id;
    params[@"size"] =self.shoes_size;
    params[@"type"] =self.type;
    [TZHttpTool post:ConfirmAPI params:params success:^(NSDictionary *result) {
        self.oderModel = [[HJOderModel alloc]init];
        self.oderModel = [HJOderModel mj_objectWithKeyValues:result[@"data"]];
        self.header.model = self.oderModel;
        self.bottom.model = self.oderModel;
        self.bottom.model.currentTag = 100;
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}


-(void)configScroll {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, mScreenHeight)];
    self.scrollView.bounces = YES;
    self.scrollView.clipsToBounds = YES;
    self.scrollView.contentSize = CGSizeMake(mScreenWidth, 320 +415+200);
    self.scrollView.backgroundColor = HJColorRGB(246);
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:_scrollView];
    
    self.header = [[HJSellOderHeader alloc]init];
    self.header.frame = CGRectMake(0, 0, mScreenWidth, 320);
    self.header.currentTag = [self.currentPush integerValue];
    self.header.size = self.shoes_size;
    MJWeakSelf
    self.header.block = ^(NSInteger tag) {
        _currentTag = tag;
         [weakSelf.bottom removeFromSuperview];
         [weakSelf configBuyBottom:tag];
    };
    [self.scrollView addSubview:self.header];
    
    [self configBuyBottom:[self.currentPush integerValue]];
}

-(void)configBuyBottom:(NSInteger)tag {
    self.bottom = [[HJSellOderBottom alloc]init];
    if (tag==100) {
        self.bottom.frame = CGRectMake(0, CGRectGetMaxY(self.header.frame), mScreenWidth, 415);
         [self.bottom.sellBtn setTitle:@"去上架" forState:UIControlStateNormal];
    }else {
        self.bottom.frame = CGRectMake(0, CGRectGetMaxY(self.header.frame), mScreenWidth, 325);
        [self.bottom.sellBtn setTitle:@"出售" forState:UIControlStateNormal];
    }
    self.bottom.tag = tag;
    self.bottom.model = self.oderModel;
    self.bottom.is_sell = NO;
    [self.scrollView addSubview:self.bottom];
}

-(void)textFieldNotification:(NSNotification *)not {
    NSString *earnings = not.userInfo[@"text"];
    if ([earnings isEqualToString:@""]) {
        self.bottom.earnings.text = @"￥0.00";
        return;
    }
    NSString *text = [earnings stringByReplacingOccurrencesOfString:@"￥" withString:@""];
    self.bottom.earnings.text = [NSString stringWithFormat:@"￥%.2f",text.integerValue - (text.integerValue * 0.05)-self.oderModel.serves_fee.integerValue];
    self.shoes_cost = earnings;
}

-(void)codeBtnNotification:(NSNotification *)not {
    self.needPopCode = YES;
    self.codeView.hidden =NO;
    self.backView.hidden = NO;
    self.codeView.size = self.shoes_size;
    self.codeView.shoes_id = self.shoes_id.numberValue;
    [self.codeView loadSizeWorkWithShoesid:self.shoes_id.numberValue size:self.shoes_size];
}

- (void)CodeViewDidClickOKButton:(NSString *)size shoes_id:(NSNumber *)shoes_id cost:(NSString *)cost type:(NSNumber *)type {
    self.needPopCode = NO;
    self.codeView.hidden =YES;
    self.backView.hidden =YES;
    [self.header.codeBtn setTitle:[NSString stringWithFormat:@"%@码",size] forState:UIControlStateNormal];
    self.header.reference.text = [NSString stringWithFormat:@"参考价：%@",cost];
    self.shoes_id = [NSString stringWithFormat:@"%@",shoes_id];
    self.shoes_size = size;
}

-(void)loadOrderNot:(NSNotification *)not {
    self.days = not.userInfo[@"days"];
}
-(void)loadOrderAddNotification:(NSNotification *)not {
    if (!self.select_Btn) {
        if ([HJUserManager isLoginView]) {
            return;
        }
        self.select_Btn = YES;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.select_Btn = NO;
        });
    }
        if ([self.shoes_id isEqualToString:@""]) {
            return;
        }
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"shoes_id"] = self.shoes_id;
        if ([self.bottom.sellBtn.titleLabel.text isEqualToString:@"去上架"]) {
            if (self.shoes_cost.length<1 || [self.shoes_cost isEqualToString:@"￥"]) {
                [self showErrorHUDWithError:@"请输入价格"];
                return;
            }
            params[@"type"] = @"1";
            params[@"shoes_size"] = self.header.size;
            params[@"shoes_cost"] = [self.shoes_cost stringByReplacingOccurrencesOfString:@"￥" withString:@""];;
            params[@"days"] = self.days;
        }else {
            params[@"type"] = @"2";
            params[@"want_buy_id"] = self.oderModel.want_buy.want_buy_id;
        }
        [self loadOrderAddWork:params];
        
}

-(void)loadOrderAddWork:(NSDictionary *)params {
   
    [TZHttpTool post:OrderAddAPI params:params success:^(NSDictionary *result) {
        [self showHint:@"提交成功"];
        if ([params[@"type"] isEqualToString:@"1"]) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                HJPayViewController * payVC = [[HJPayViewController alloc]init];
                payVC.navTitle = @"支付中心";
                payVC.order_sn = result[@"data"][@"sell_id"];
                payVC.pay = result[@"data"][@"pay"];
                payVC.order_type = @"1";
                [self.navigationController pushViewController:payVC animated:YES];
            });
        }else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
@end
